package com.itheima.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.leyou.common.exception.LyException;
import com.itheima.leyou.entity.ClientInfo;
import com.itheima.leyou.mapper.AuthMapper;
import com.itheima.leyou.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, ClientInfo> implements AuthService {

  private final PasswordEncoder passwordEncoder;

   @Value("${ly.jwt.key}")
    private String key;

    public AuthServiceImpl(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public String checkClient(String clientId, String secret) {
        //校验用户端传过来的数据是否正确
        ClientInfo clientInfo = query().eq("client_id", clientId).one();
                if(clientInfo==null||(!passwordEncoder.matches(secret,clientInfo.getSecret()))){
                    throw new LyException(403,"验证失败");
                }
                return key;
    }
}
